package pre.lxm.demo.chart.horizontal;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;

/**
 * Created by lxm on 2017/6/14.
 */

public class ChartLinearLayout extends LinearLayout {

    private Context mContext;
    private NoScrollRecordChartView noScrollChart;

    public ChartLinearLayout(Context context) {
        this(context, null);
    }

    public ChartLinearLayout(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ChartLinearLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mContext = getContext();
        setOrientation(HORIZONTAL);

        LeftLinearLayout leftLl = new LeftLinearLayout(mContext);
        LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup
                .LayoutParams.MATCH_PARENT);

        leftLl.setLayoutParams(llp);
        addView(leftLl);
        addChartView();
    }

    private void addChartView() {
        HorizontalScrollView horizontalScrollView = new HorizontalScrollView(mContext);

        LinearLayout ll = new LinearLayout(mContext);
        ll.setOrientation(HORIZONTAL);
        noScrollChart = new NoScrollRecordChartView(mContext);
        LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT);
        noScrollChart.setLayoutParams(llp);
        ll.addView(noScrollChart);
        horizontalScrollView.addView(ll);

        addView(horizontalScrollView);
    }

    public NoScrollRecordChartView getRecordChartView() {
        return noScrollChart;
    }

    private class LeftLinearLayout extends View {

        private Paint mPaint;
        private TextPaint mTextPaint;
        private int mHeight;
        private int lineHeight;
        private int itemHeight;
        private int itemWidth;
        private int leftPadding;

        private String[] resultStr = new String[]{"胜", "平", "负"};

        public LeftLinearLayout(Context context) {
            super(context);
            itemWidth = getScreenWidth(getContext()) / 8;
            lineHeight = dip2px(1);
            leftPadding = dip2px(16);
            initPaint();
            initTextPaint();
        }

        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);

            // 绘制横线
            mPaint.setStrokeWidth(lineHeight);
            mPaint.setColor(Color.parseColor("#E1E1E1"));
            canvas.drawLine(0, lineHeight, itemWidth, lineHeight, mPaint);
            canvas.drawLine(0, lineHeight + itemHeight, itemWidth, lineHeight + itemHeight, mPaint);


            // 轮次栏底色
            Rect turnRect = new Rect(0, lineHeight, itemWidth, lineHeight + itemHeight);
            mPaint.setColor(Color.parseColor("#F5F5F5"));
            mPaint.setStyle(Paint.Style.FILL);
            canvas.drawRect(turnRect, mPaint);

            // 轮次
            canvas.save();
            // canvas.translate(leftPadding, 0);
            mTextPaint.setColor(Color.parseColor("#888888"));
            drawText(canvas, mTextPaint, "轮次", leftPadding * 2 + itemWidth, itemHeight);
            canvas.restore();


            // 胜平负底色
            canvas.save();
            canvas.translate(0, itemHeight + lineHeight * 2);
            Rect resultRect = new Rect(0, 0, itemWidth, mHeight);
            mPaint.setColor(Color.parseColor("#FFFFFF"));
            canvas.drawRect(resultRect, mPaint);
            canvas.restore();

            // 胜平负
            canvas.save();
            mTextPaint.setColor(Color.BLACK);
            for (int i = 0; i < resultStr.length; i++) {
                canvas.translate(0, itemHeight + lineHeight * 2);
                drawText(canvas, mTextPaint, resultStr[i], leftPadding + itemWidth, itemHeight);
            }
            canvas.restore();

        }

        private Rect drawText(Canvas canvas, TextPaint textPaint, String txtString, int txtMeasureWidth,
                              int txtMeasureHeight) {
            Rect mRect = new Rect();
            textPaint.getTextBounds(txtString, 0, txtString.length(), mRect);
            Paint.FontMetricsInt fontMetrics = textPaint.getFontMetricsInt();
            float baseline = txtMeasureHeight / 2 + (fontMetrics.descent - fontMetrics.ascent) / 2 - fontMetrics
                    .descent;
            canvas.drawText(txtString, (txtMeasureWidth - mRect.width()) / 2, baseline, textPaint);
            return mRect;
        }

        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            mHeight = getMeasuredHeight();
            itemHeight = (mHeight - lineHeight * 2) / 4;
            setMeasuredDimension(itemWidth, mHeight);
        }

        private void initPaint() {
            mPaint = new Paint();
            mPaint.setColor(Color.parseColor("#E1E1E1"));
            mPaint.setStyle(Paint.Style.FILL);
            mPaint.setStrokeWidth(lineHeight);
        }

        private void initTextPaint() {
            mTextPaint = new TextPaint();
            mTextPaint.setTextSize(sp2px(14));
            mTextPaint.setStrokeWidth(2);
            mTextPaint.setTextAlign(Paint.Align.CENTER);
            mTextPaint.setStyle(Paint.Style.FILL);
        }

        private int sp2px(float spValue) {
            final float fontScale = getContext().getResources().getDisplayMetrics().scaledDensity;
            return (int) (spValue * fontScale + 0.5f);
        }

        private int dip2px(float dipValue) {
            final float scale = getContext().getResources().getDisplayMetrics().density;
            return (int) (dipValue * scale + 0.5f);
        }

        private int getScreenWidth(Context context) {
            DisplayMetrics dm = context.getResources().getDisplayMetrics();
            return dm.widthPixels;
        }
    }
}
